package com.yaozheng.vocationaltraining.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.bean.PKRankBean;
import com.yaozheng.vocationaltraining.bean.PKResultBean;
import com.yaozheng.vocationaltraining.bean.PKResultPlayerBean;
import com.yaozheng.vocationaltraining.utils.BitmapLoader;
import com.yaozheng.vocationaltraining.view.ExamProgressStatusView_;

import java.util.List;

/**
 * Created by moon on 2017/11/19.
 */

public class PKResultListAdapter extends BaseAdapter {
    private LayoutInflater mInflater;//
    private Activity mContext;
    private BitmapLoader userBitmapLoader;

    private List<PKResultPlayerBean> data = null;

    private int selectedPosition = -1;

    public PKResultListAdapter(Activity context,
                         List<PKResultPlayerBean> data) {
        this.mInflater = LayoutInflater.from(context);
        this.mContext = context;
        this.data = data;
        userBitmapLoader = new BitmapLoader(Glide.with(context), R.drawable.user_img);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(
                    R.layout.pk_result_list_item, null);
            holder = new ViewHolder();

            holder.root_layout = (LinearLayout) convertView.findViewById(R.id.root_layout);
            holder.win_imageview = (ImageView) convertView.findViewById(R.id.win_imageview);
            holder.userAvatarImage = (ImageView) convertView
                    .findViewById(R.id.userAvatarImage);
            holder.name_textview = (TextView) convertView
                    .findViewById(R.id.name_textview);

            holder.position_textview = (TextView) convertView
                    .findViewById(R.id.position_textview);
            holder.store_name_textview = (TextView) convertView
                    .findViewById(R.id.store_name_textview);
            holder.examProgressStatusView = (ExamProgressStatusView_) convertView
                    .findViewById(R.id.examProgressStatusView);
            holder.first_cell_score_layout = (LinearLayout) convertView.findViewById(R.id.first_cell_score_layout);
            holder.first_cell_score_textview = (TextView) convertView.findViewById(R.id.first_cell_score_textview);


            convertView.setTag(holder);//
        } else {
            holder = (ViewHolder) convertView.getTag();//
        }

        if(data.get(position).isResult()){
            holder.win_imageview.setVisibility(View.VISIBLE);
            holder.first_cell_score_layout.setVisibility(View.VISIBLE);
            holder.examProgressStatusView.setVisibility(View.GONE);
            holder.root_layout.setBackground(mContext.getResources().getDrawable(R.drawable.pk_rank_first_bg_color ));

            holder.name_textview.setTextColor(mContext.getResources().getColor(R.color.default_bg_color));
            holder.position_textview.setTextColor(mContext.getResources().getColor(R.color.default_bg_color));
            holder.store_name_textview.setTextColor(mContext.getResources().getColor(R.color.default_bg_color));

        }else{
            holder.win_imageview.setVisibility(View.GONE);
            holder.first_cell_score_layout.setVisibility(View.GONE);
            holder.examProgressStatusView.setVisibility(View.VISIBLE);
            holder.root_layout.setBackgroundColor(mContext.getResources().getColor(R.color.default_bg_color));

            holder.name_textview.setTextColor(mContext.getResources().getColor(R.color.black));
            holder.position_textview.setTextColor(mContext.getResources().getColor(R.color.black));
            holder.store_name_textview.setTextColor(mContext.getResources().getColor(R.color.black));
        }

        //加载图片
        userBitmapLoader.displayImage(data.get(position).getPlayerAvatar(), holder.userAvatarImage);
        holder.name_textview.setText(data.get(position).getPlayerName());
        holder.position_textview.setText(data.get(position).getPlayerPosition());
        holder.store_name_textview.setText(data.get(position).getPlayerStoreName());
        holder.examProgressStatusView.setStatus(data.get(position).getScore());




        return convertView;
    }

    public void setData(List<PKResultPlayerBean> data) {
        this.data = data;
        PKResultPlayerBean winPlayer = null;
        for (int i=0 ; i< data.size() ; i++){
            if(this.data.get(i).isResult()){
                winPlayer = this.data.get(i);
            }
        }
        if(winPlayer != null){
            this.data.remove(winPlayer);
            this.data.add(0,winPlayer);
        }
    }

    public void setselectedPosition(int selectedPosition) {
        notifyDataSetChanged();;
    }

    class ViewHolder {
        ImageView win_imageview;
        ImageView userAvatarImage;
        TextView name_textview;
        TextView position_textview;
        TextView store_name_textview;
        ExamProgressStatusView_ examProgressStatusView;
        LinearLayout first_cell_score_layout;
        TextView first_cell_score_textview;


        LinearLayout root_layout;
    }
}
